﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PKURG.CostSplit.Business;
using System.IO;
using System.Data;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;

public partial class ImportTable : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int id = 0;
        string idString = Request["Id"];
        if (string.IsNullOrEmpty(idString) || !int.TryParse(idString, out id))
        {
            Common.GoErrorPage();
            return;
        }

        List<dynamic> stageInfos = Project.GetProjectStageInfo(id);
        if (stageInfos.Count <= 0 || stageInfos[0].rCount == "0")
        {
            Common.GoErrorPage();
            return;
        }

        string ProjectName = stageInfos[0].ProjectName;
        string StageName = stageInfos[0].StageName;

        string fileName = string.Format("{0}({1})", ProjectName, StageName);
        string excelFilePath = Path.Combine(Server.MapPath("~/Files/Import/"), fileName + ".xls");
        UploadFile.PostedFile.SaveAs(fileName);

        DataTable dt = new DataTable();
        using (FileStream fs = new FileStream(excelFilePath, FileMode.Open, FileAccess.Read))
        {
            HSSFWorkbook hssfworkbook = new HSSFWorkbook(fs);
            ISheet sheet = hssfworkbook.GetSheetAt(0);

            //表头  
            IRow header = sheet.GetRow(sheet.FirstRowNum);

            Dictionary<Guid, decimal> updateDatas = new Dictionary<Guid, decimal>();


            for (int rowIndex = sheet.FirstRowNum + 1; rowIndex <= sheet.LastRowNum; rowIndex++)
            {
                for (int colIndex = 0; colIndex < header.LastCellNum; colIndex++)
                {
                    string dsIdString = sheet.GetRow(rowIndex).Cells[colIndex].StringCellValue;
                    Guid dsId = Guid.Empty;
                    bool isData = Guid.TryParse(dsIdString, out dsId);
                    if (!isData)
                    {
                        continue;
                    }

                    string valueString = sheet.GetRow(rowIndex).Cells[colIndex + 1].StringCellValue;
                    decimal value = decimal.Parse(valueString == "" ? "0" : valueString);
                    updateDatas.Add(dsId, value);
                }
            }

            UpDateDatas(id, updateDatas);

        }

    }

    private void UpDateDatas(int stageId, Dictionary<Guid, decimal> updateDatas)
    {
        List<dynamic> detailInfos = DetailSplit.GetDetailSplitInfoAtOnce(stageId, 1);

        foreach (var item in updateDatas)
        {
            dynamic detailInfo = detailInfos.Where(x => x.Id == item.Key.ToString()).SingleOrDefault();
            if (detailInfo == null)
            {
                //保存失败
            }
            if (detailInfo.Value != item.Value.ToString())
            {//只更新变更值
                DetailSplit.UpDateDetailItemInfo(item.Key, item.Value);
            }
        }
    }
}